/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui.form;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.SwingConstants;

import Control.StringValidator;
import Model.Pet;
import dao.DaoPet;

/**
 * 
 * @author Daniel Bustos
 */
public class PetPanel extends javax.swing.JPanel {

	private static final long serialVersionUID = 1L;

	private boolean isCreating;

	public PetPanel() {
		initComponents();
		isCreating = true;
	}

	public PetPanel(Pet pet) {
		initComponents();
		isCreating = false;
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed"
	// desc="Generated Code">//GEN-BEGIN:initComponents
	private void initComponents() {

		nameField = new javax.swing.JTextField();
		txtAge = new javax.swing.JTextField();
		txtSpecie = new javax.swing.JTextField();
		jButton2 = new javax.swing.JButton();
		jButton2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				Pet pet = new Pet();
				if (!StringValidator.checkSpecialChar(nameField.getText())
						&& StringValidator.notEmpty(nameField.getText())) {
					pet.setName(nameField.getText());
				} else {
					notifyUser("Please check the Name field, aren't allowed special characters or empty valors!");
					return;
				}

				if (!StringValidator.checkSpecialChar(txtAge.getText())
						&& StringValidator.notEmpty(txtAge.getText())) {
					try {
						pet.setAge(Integer.valueOf(txtAge.getText()));
					} catch (Exception mistake) {
						notifyUser("Error: " + mistake.getMessage());
						return;
					}
				} else {
					notifyUser("Please check the Age field, aren't allowed special characters or empty valors!");
					return;
				}

				if (!StringValidator.checkSpecialChar(txtSpecie.getText())
						&& StringValidator.notEmpty(txtSpecie.getText())) {
					pet.setSpecies(txtSpecie.getText());
				} else {
					notifyUser("Please check the Specie field, aren't allowed special characters or empty valors!");
					return;
				}

				if (!StringValidator.checkSpecialChar(txtWeight.getText())
						&& StringValidator.notEmpty(txtWeight.getText())) {
					try {
						pet.setWeight(Float.valueOf(txtWeight.getText()));
					} catch (Exception mistake) {
						notifyUser("Error: " + mistake.getMessage());
						return;
					}
				} else {
					notifyUser("Please check the Weight field, aren't allowed special characters or empty valors!");
					return;
				}

				String petImage = (String) comboPictures.getSelectedItem();

				for (Entry<String, String> photo : imagesMap.entrySet()) {
					if (petImage.equals(photo.getKey())) {
						String path = photo.getValue();
						pet.setPicture(path);
					}

				}

				DaoPet daoPet = new DaoPet();

				if (isCreating) {
					daoPet.insertEntity(pet);
				} else {
					daoPet.updateEntity(pet);
				}

				System.out.println();

				clearTextbox();

			}
		});
		txtWeight = new javax.swing.JTextField();
		jLabel1 = new javax.swing.JLabel();
		jLabel2 = new javax.swing.JLabel();
		jLabel3 = new javax.swing.JLabel();
		jLabel4 = new javax.swing.JLabel();
		jLabel5 = new javax.swing.JLabel();

		jButton2.setText("Save");

		jLabel1.setText("Name:");

		jLabel2.setText("Specie");

		jLabel3.setText("Age:");

		jLabel4.setText("Weight:");

		jLabel5.setText("Please select a picture:");

		lblPet = new JLabel("PET");
		lblPet.setHorizontalAlignment(SwingConstants.CENTER);
		lblPet.setFont(new Font("Tahoma", Font.BOLD, 15));

		comboPictures = new JComboBox();
		comboPictures.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {

				String petName = (String) comboPictures.getSelectedItem();

				for (Entry<String, String> e : imagesMap.entrySet()) {
					if (petName.equals(e.getKey())) {
						String path = e.getValue();
						URL url = this.getClass().getResource(path);
						ImageIcon icon = new ImageIcon(url);
						labelImage.setIcon(icon);

					}

					// System.out.println("["+e.getKey() + "=" +
					// e.getValue()+"]");
				}
			}
		});

		labelImage = new JLabel("");

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
		layout.setHorizontalGroup(layout
				.createParallelGroup(Alignment.TRAILING)
				.addGroup(
						Alignment.LEADING,
						layout.createSequentialGroup()
								.addGap(80)
								.addGroup(
										layout.createParallelGroup(
												Alignment.LEADING)
												.addComponent(jLabel1)
												.addComponent(jLabel2)
												.addComponent(jLabel3)
												.addComponent(jLabel4)
												.addComponent(jLabel5))
								.addGap(47)
								.addGroup(
										layout.createParallelGroup(
												Alignment.LEADING, false)
												.addComponent(
														txtWeight,
														GroupLayout.DEFAULT_SIZE,
														124, Short.MAX_VALUE)
												.addComponent(
														txtAge,
														GroupLayout.DEFAULT_SIZE,
														124, Short.MAX_VALUE)
												.addComponent(
														txtSpecie,
														GroupLayout.DEFAULT_SIZE,
														124, Short.MAX_VALUE)
												.addComponent(
														nameField,
														GroupLayout.DEFAULT_SIZE,
														124, Short.MAX_VALUE)
												.addComponent(
														comboPictures,
														0,
														GroupLayout.DEFAULT_SIZE,
														Short.MAX_VALUE))
								.addGap(18)
								.addComponent(labelImage,
										GroupLayout.PREFERRED_SIZE, 109,
										GroupLayout.PREFERRED_SIZE)
								.addContainerGap(31, Short.MAX_VALUE))
				.addGroup(
						layout.createSequentialGroup()
								.addContainerGap(310, Short.MAX_VALUE)
								.addComponent(jButton2).addGap(90))
				.addGroup(
						layout.createSequentialGroup()
								.addContainerGap()
								.addComponent(lblPet,
										GroupLayout.PREFERRED_SIZE, 437,
										GroupLayout.PREFERRED_SIZE)
								.addContainerGap(GroupLayout.DEFAULT_SIZE,
										Short.MAX_VALUE)));
		layout.setVerticalGroup(layout
				.createParallelGroup(Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addGap(10)
								.addComponent(lblPet,
										GroupLayout.PREFERRED_SIZE, 25,
										GroupLayout.PREFERRED_SIZE)
								.addGap(18)
								.addGroup(
										layout.createParallelGroup(
												Alignment.TRAILING)
												.addGroup(
														layout.createSequentialGroup()
																.addGroup(
																		layout.createParallelGroup(
																				Alignment.BASELINE)
																				.addComponent(
																						jLabel1)
																				.addComponent(
																						nameField,
																						GroupLayout.PREFERRED_SIZE,
																						GroupLayout.DEFAULT_SIZE,
																						GroupLayout.PREFERRED_SIZE))
																.addPreferredGap(
																		ComponentPlacement.UNRELATED)
																.addGroup(
																		layout.createParallelGroup(
																				Alignment.BASELINE)
																				.addComponent(
																						jLabel2)
																				.addComponent(
																						txtSpecie,
																						GroupLayout.PREFERRED_SIZE,
																						GroupLayout.DEFAULT_SIZE,
																						GroupLayout.PREFERRED_SIZE))
																.addPreferredGap(
																		ComponentPlacement.UNRELATED)
																.addGroup(
																		layout.createParallelGroup(
																				Alignment.BASELINE)
																				.addComponent(
																						txtAge,
																						GroupLayout.PREFERRED_SIZE,
																						GroupLayout.DEFAULT_SIZE,
																						GroupLayout.PREFERRED_SIZE)
																				.addComponent(
																						jLabel3))
																.addPreferredGap(
																		ComponentPlacement.UNRELATED)
																.addGroup(
																		layout.createParallelGroup(
																				Alignment.LEADING)
																				.addComponent(
																						txtWeight,
																						GroupLayout.PREFERRED_SIZE,
																						GroupLayout.DEFAULT_SIZE,
																						GroupLayout.PREFERRED_SIZE)
																				.addComponent(
																						jLabel4,
																						Alignment.TRAILING)))
												.addComponent(
														labelImage,
														GroupLayout.PREFERRED_SIZE,
														118,
														GroupLayout.PREFERRED_SIZE))
								.addGroup(
										layout.createParallelGroup(
												Alignment.LEADING)
												.addGroup(
														layout.createSequentialGroup()
																.addGap(24)
																.addComponent(
																		jLabel5))
												.addGroup(
														layout.createSequentialGroup()
																.addGap(18)
																.addComponent(
																		comboPictures,
																		GroupLayout.PREFERRED_SIZE,
																		GroupLayout.DEFAULT_SIZE,
																		GroupLayout.PREFERRED_SIZE)))
								.addGap(27).addComponent(jButton2)
								.addContainerGap(70, Short.MAX_VALUE)));
		this.setLayout(layout);

		fillCombo();
	}// </editor-fold>//GEN-END:initComponents

	private void notifyUser(String notification) {
		JOptionPane.showMessageDialog(this, notification);
	}

	void clearTextbox() {

		nameField.setText("");
		txtAge.setText("");
		txtSpecie.setText("");
		txtWeight.setText("");

	}

	private void fillCombo() {

		imagesMap.put("bee", "/images/abeja.jpg");
		imagesMap.put("spider", "/images/arana.jpg");
		imagesMap.put("squirrel", "/images/ardilla.jpg");
		imagesMap.put("fish", "/images/pez.jpg");
		imagesMap.put("chamaleon", "/images/camaleon.jpg");
		imagesMap.put("kangaroo", "/images/canguro.jpg");
		imagesMap.put("rabbit", "/images/conejo.jpg");
		imagesMap.put("scorpion", "/images/escorpion.jpg");
		imagesMap.put("giraffe", "/images/girafa.jpg");
		imagesMap.put("gorilla", "/images/gorila.jpg");
		imagesMap.put("hamster", "/images/hamster.jpg");
		imagesMap.put("ant", "/images/hormiga.jpg");
		imagesMap.put("monkey", "/images/mono.jpg");
		imagesMap.put("bear", "/images/oso.jpg");
		imagesMap.put("sheep", "/images/oveja.jpg");
		imagesMap.put("bird", "/images/pajaro.jpg");
		imagesMap.put("dog 1", "/images/perro.jpg");
		imagesMap.put("dog 2", "/images/perro2.jpg");
		imagesMap.put("dog 3", "/images/perro3.jpg");
		imagesMap.put("dog 4", "/images/parro4.jpg");
		imagesMap.put("cat", "/images/gato.jpg");
		imagesMap.put("mouse", "/images/raton.jpg");
		imagesMap.put("frog", "/images/sapo.jpg");

		for (Entry<String, String> e : imagesMap.entrySet()) {
			comboPictures.addItem(e.getKey());
			// System.out.println("["+e.getKey() + "=" + e.getValue()+"]");
		}

	}

	private javax.swing.JButton jButton2;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JLabel jLabel3;
	private javax.swing.JLabel jLabel4;
	private javax.swing.JLabel jLabel5;
	private javax.swing.JTextField nameField;
	private javax.swing.JTextField txtSpecie;
	private javax.swing.JTextField txtAge;
	private javax.swing.JTextField txtWeight;
	private JLabel lblPet;
	private JLabel labelImage;
	private JComboBox comboPictures;
	private Map<String, String> imagesMap = new HashMap<String, String>();
}
